package com.jd.finance.core.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.finance.core.mapper.AdminApplyMapper;
import com.jd.finance.core.mapper.ContractMapper;
import com.jd.finance.core.mapper.UserMapper;
import com.jd.finance.core.pojo.entity.AdminApply;
import com.jd.finance.core.pojo.entity.Contract;
import com.jd.finance.core.pojo.entity.User;
import com.jd.finance.core.service.AdminApplyService;
import com.jd.finance.core.service.OpreateLogService;
import com.jd.finance.result.BaseResult;
import com.jd.finance.util.IpUtil;
import com.jd.finance.util.MD5;
import com.jd.finance.util.RegexValidateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 后台账号申请表 服务实现类
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-28
 */
@Service
public class AdminApplyServiceImpl extends ServiceImpl<AdminApplyMapper, AdminApply> implements AdminApplyService {

    @Resource
    private AdminUtilsServiceImpl adminUtilsService;

    @Resource
    private AdminApplyMapper adminApplyMapper;

    @Resource
    private OpreateLogService opreateLogService;

    @Resource
    private UserMapper userMapper;

    @Resource
    private ContractMapper contractMapper;
    /**
     * 添加账号申请信息
     * @param uid
     * @param userPhone
     * @param userPassword
     * @param userCon
     * @param request
     * @return
     */
    @Override
    public BaseResult adminAccountApply(Integer uid, String userPhone, String userPassword, String userName,String userCon, HttpServletRequest request) {
        if (StringUtils.isAllBlank(String.valueOf(uid),userPhone,userPassword,userName,userCon)) {
            log.error("提交信息为空!");
            return BaseResult.error().message("提交信息为空!");
        }

        //判断用户身份
        if (!adminUtilsService.isHR(uid)) {
            log.error("该用户非HR身份!");
            return BaseResult.error().message("该用户非HR身份!");
        }

        //判断手机号格式
        boolean phone=RegexValidateUtils.checkCellphone(userPhone);
        if (!phone) {
            log.error("用户输入的手机号格式不正确!");
            return BaseResult.error().message("用户输入的手机号格式不正确!");
        }

        //密码长度不能少于八位
        if (userPassword.length() < 8) {
            log.error("密码长度不能少于8位");
            return BaseResult.error().message("密码长度不能少于8位");

        }

        //密码加密
        String password= MD5.encrypt(userPassword);

        AdminApply adminApply=new AdminApply();
        adminApply.setUserPhone(userPhone);
        adminApply.setUserPassword(password);
        adminApply.setUserName(userName);
        adminApply.setUserConUrl(userCon);
        adminApply.setAdminApplyStatus("0");

        int insert=adminApplyMapper.insert(adminApply);
        if (insert < 1) {
            log.error("添加失败!");
            return BaseResult.ok().data("shenqingjieguo",0).message("添加失败!");
        }

        boolean add=opreateLogService.add(uid,"添加","添加申请账号信息", IpUtil.getIpAddr(request));
        if (!add) {
            log.error("添加操作日志失败!");
        }
        return BaseResult.ok().data("shenqingjieguo",1).message("添加成功!");
    }

    /**
     * 总经理 查询账号申请
     * @param pageParam
     * @param uid
     * @return
     */
    @Override
    public Page<AdminApply> getAccountApplyInfo(Page<AdminApply> pageParam, Integer uid) {
        if (uid==null) {
            log.error("uid为空！");
        }
        return adminApplyMapper.selectPage(pageParam,null);
    }

    /**
     * 根据账号申请id，查询申请详情
     * @param uid
     * @param accApplyId
     * @return
     */
    @Override
    public BaseResult getAccountAppInfoById(Integer uid, Integer accApplyId) {
        if (uid==null || accApplyId==null) {
            log.error("信息为空!");
            return BaseResult.error().message("信息为空!");
        }

        if (!adminUtilsService.isGM(uid)) {
            log.error("该用户非总经理身份!");
            return BaseResult.error().message("该用户非总经理身份!");
        }

        AdminApply adminApply=adminApplyMapper.selectById(accApplyId);
        if (adminApply==null) {
            log.error("未查询到相关信息!");
            return BaseResult.error().message("未查询到相关信息!");
        }
        Map<String,Object> result=new HashMap<>();
        result.put("shenqingid",adminApply.getAdminApplyId());
        result.put("time",adminApply.getCreateTime());
        result.put("phone",adminApply.getUserPhone());
        result.put("name",adminApply.getUserName());
        result.put("xieyi",adminApply.getUserConUrl());

        return BaseResult.ok().data(result);
    }

    /**
     * 审核账号申请(修改审核状态)
     * @param uid
     * @param accAppId
     * @param request
     * @return
     */
    @Override
    public BaseResult checkAccountApply(Integer uid, Integer accAppId, String applyStatus,HttpServletRequest request) {
        if (StringUtils.isAllBlank(String.valueOf(uid),String.valueOf(accAppId),applyStatus)) {
            log.error("提交信息为空!");
            return BaseResult.error().message("提交信息为空!");
        }

        if (!adminUtilsService.isGM(uid)) {
            log.error("该用户非总经理身份!");
            return BaseResult.error().message("该用户非总经理身份!");
        }

        AdminApply adminApply=new AdminApply();
        if (applyStatus.equals("通过")) {
            adminApply.setAdminApplyStatus("1");
        }else if (applyStatus.equals("未通过")) {
            adminApply.setAdminApplyStatus("2");
        }else {
            log.error("信息错误!");
            return BaseResult.error().message("信息错误!");
        }

        QueryWrapper<AdminApply> adminApplyQueryWrapper = new QueryWrapper<>();
        adminApplyQueryWrapper.eq("admin_apply_id",accAppId);
        int update=adminApplyMapper.update(adminApply,adminApplyQueryWrapper);

        if (update < 1) {
            log.error("更新失败!");
            return BaseResult.ok().data("shenhejieguo",0).message("更新失败!");
        }

        boolean add=opreateLogService.add(uid,"修改","修改账号申请状态",IpUtil.getIpAddr(request));
        if (!add) {
            log.error("添加操作日志失败!");
        }

        return BaseResult.ok().data("shenhejieguo",1).message("审核成功!");
    }

    /**
     * 添加后台账号
     * @param uid
     * @param accAppId
     * @param request
     * @return
     */
    @Transactional
    @Override
    public BaseResult addAdminAccount(Integer uid, Integer accAppId, HttpServletRequest request) {
        if (uid==null || accAppId==null) {
            log.error("提交信息为空!");
            return BaseResult.error().message("提交信息为空!");
        }

        if (!adminUtilsService.isHR(uid)) {
            log.error("该用户非HR身份！");
            return BaseResult.error().message("该用户非HR身份！");
        }

        AdminApply adminApply=adminApplyMapper.selectById(accAppId);
        if (adminApply==null) {
            log.error("未查到相关申请！");
            return BaseResult.error().message("未查到相关申请！");
        }

        if (adminApply.getAdminApplyStatus().equals("1")) {
            User user=new User();
            user.setUserAccount(adminApply.getUserPhone());
            user.setUserPassword(adminApply.getUserPassword());
            user.setUserPhone(adminApply.getUserPhone());
            user.setUserName(adminApply.getUserName());
            user.setAdmin(true);

            int insert=userMapper.insert(user);
            if (insert < 1) {
                log.error("添加失败!");
                return BaseResult.ok().data("tianjiajieguo",0);
            }

            Contract contract=new Contract();
            contract.setUid(user.getUid());
            contract.setContName(adminApply.getUserName());
            contract.setContPhone(adminApply.getUserPhone());
            contract.setContInfo(adminApply.getUserConInfo());
            contract.setContUrl(adminApply.getUserConUrl());

            int insert1=contractMapper.insert(contract);
            if (insert1 < 1) {
                log.error("添加失败!");
                return BaseResult.ok().data("tianjiajieguo",0);
            }

            boolean add=opreateLogService.add(uid,"添加","添加后台账号",IpUtil.getIpAddr(request));
            if (!add) {
                log.error("添加操作日志失败!");
            }

            return BaseResult.ok().data("tianjiajieguo",1);
        }else if (adminApply.getAdminApplyStatus().equals("2")) {
            log.error("该申请审核为未通过!");
            return BaseResult.error().message("该申请审核为未通过!");
        }else {
            return BaseResult.error().message("信息错误！");
        }

    }
}
